﻿#pragma once
#pragma execution_character_set("utf-8")
#include "DataBase.h"
#include "GetString.h"
#include <iostream>

static Database* _Database = nullptr;

const char* gb23122utf8(const char* gb2312)
{
	int len = MultiByteToWideChar(0, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(0, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(65001, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(65001, 0, wstr, -1, str, len, NULL, NULL);
	if (wstr) delete[] wstr;
	return str;
}

Database::Database()
{
	//server = "127.0.0.1";
	//user = "root";
	//password = "admin";
	//port = 3306;
	server = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("DataBase_Host");
	user = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("DataBase_UserName");
	password = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("DataBase_PassWord");
	std::string text = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("DataBase_Port");
	port = atoi(text.c_str());

	database = "world";
	Databasestarttime = 0;
	DataBaseHodingTimer = 5000;
	std::thread th(&Database::ConnectDatabaes, this);
	th.detach();
}

Database::~Database()
{

}

void Database::PExcude(DataBaseType type, const char* args, ...)
{
	char* _database;
	switch (type)
	{
	case WorldDatabase:
		_database = "world";
		break;
	case LoginDataBase:
		break;
	case CharacterDatabase:
		_database = "characters";
		break;
	default:
		break;
	}
	MYSQL mysql;//连接数据库的变量
	int t;
	mysql_init(&mysql);

	if (!mysql_real_connect(&mysql, server.c_str(), user.c_str(), password.c_str(), _database, port, NULL, 0))
	{
		mysql_close(&mysql);
		return;
	}
	mysql_query(&mysql, "SET NAMES UTF8");
	t = mysql_real_query(&mysql, args, (unsigned int)strlen(args));
	mysql_close(&mysql);
}

void Database::GetUpdate()
{

}

void Database::StartDataBaseHoding(int diff)
{

}

bool Database::ConnectDatabaes()
{
	MYSQL myCont;
	MYSQL_RES *result;
	int res;
	int t;
	mysql_init(&myCont);
	if (mysql_real_connect(&myCont, server.c_str(), user.c_str(), password.c_str(), database.c_str(), port, NULL, 0))
	{
		t = mysql_real_query(&myCont, "SELECT 1 FROM account", (unsigned int)strlen("SELECT 1 FROM account"));
		if (t)
			return 0;
		else;
		{
			while (true)
			{
				char msg[1000];
				_snprintf_c(msg, 1000, "SELECT 1 FROM account");
				log("holding");
				res = mysql_query(&myCont, msg);
				if (!res)
					result = mysql_store_result(&myCont);
				if (result)
				{
					//mysql_free_result(result);
				}
				Sleep(5000);
			}
		}
	}
	return true;
}

Database* Database::GetInstance()
{
	if (_Database == nullptr)
	{
		_Database = new Database();
		GetInstance();
	}
	return _Database;
}

Result::Qurey Database::GetResult(DataBaseType type, const char* args,...)
{
	char* _database;
	switch (type)
	{
	case WorldDatabase:
		_database = "world";
		break;
	case LoginDataBase:
		break;
	case CharacterDatabase:
		_database = "characters";
		break;
	default:
		break;
	}
	Result::Qurey sqlresult;
	sqlresult.clear();
	MYSQL mysql;//连接数据库的变量
	MYSQL_RES *res;//存放查询结果的变量
	MYSQL_ROW row;
	int t;
	mysql_init(&mysql);
	if (!mysql_real_connect(&mysql, server.c_str(), user.c_str(), password.c_str(), _database, port, NULL, 0))
	{
		mysql_close(&mysql);
		return sqlresult;
	}
	mysql_query(&mysql, "SET NAMES UTF8");
	t = mysql_real_query(&mysql, args, (unsigned int)strlen(args));
	if (t)
	{
		return sqlresult;
	}
	else
		res = mysql_store_result(&mysql);
	int i = 0;
	while (row = mysql_fetch_row(res))
	{
		std::vector<string> _vector;
		for (t = 0; t <= mysql_num_fields(res); t++)
		{
			_vector.push_back(row[t]);
		}
		sqlresult[i] = _vector;
		i++;
	}
	mysql_free_result(res);
	mysql_close(&mysql);
	return sqlresult;
}

Result::Qurey Database::GetResult(DataBaseType type, char* args)
{
	char* _database;
	switch (type)
	{
	case WorldDatabase:
		_database = "world";
		break;
	case LoginDataBase:
		break;
	case CharacterDatabase:
		_database = "characters";
		break;
	default:
		break;
	}
	Result::Qurey sqlresult;
	sqlresult.clear();
	MYSQL mysql;//连接数据库的变量
	MYSQL_RES *res;//存放查询结果的变量
	MYSQL_ROW row;
	char *query;
	int t;
	mysql_init(&mysql);
	if (!mysql_real_connect(&mysql, server.c_str(), user.c_str(), password.c_str(), _database, port, NULL, 0))
	{
		mysql_close(&mysql);
		return sqlresult;
	}
	query = args;
	mysql_query(&mysql, "SET NAMES UTF8");
	t = mysql_real_query(&mysql, query, (unsigned int)strlen(query));
	if (t)
	{
		return sqlresult;
	}
	else
		res = mysql_store_result(&mysql);
	int i = 0;
	while (row = mysql_fetch_row(res))
	{
		std::vector<string> _vector;
		for (t = 0; t <= mysql_num_fields(res); t++)
		{
			_vector.push_back(row[t]);
		}
		sqlresult[i] = _vector;
		i++;
	}
	mysql_free_result(res);
	mysql_close(&mysql);
	return sqlresult;
}
void Database::LoadPlayerFromDB()
{

}
void Database::LoadCardTemplate()
{
	//_CardTemplate.clear();
	////			0	1			2					3					4				5					6					7						8								9								10								11				12			13				14		15			16
	//Result::Qurey* result = GetResult("SELECT entry,card_type,base_attack_power,base_magic_power,base_physics_defense,base_magic_defense,base_heath,reward_attack_power_per_level,reward_magic_power_per_level,reward_physics_defense_per_level,reward_magic_defense_per_level,reward_heath_per_level,card_spell,feed_reward_exp,isherocard,image_url,head_image_url FROM card_template");
	//if (result.empty()){}
	//else
	//{
	//	for (Result::Qurey::iterator itr = result.begin(); itr != result.end(); itr++)
	//	{
	//		CardInfo _CardInfo;
	//		_CardInfo.CardType = atoi(itr->second.at(1).c_str());
	//		_CardInfo.base_attack_power = atoi(itr->second.at(2).c_str());
	//		_CardInfo.base_magic_power = atoi(itr->second.at(3).c_str());
	//		_CardInfo.base_physics_defense = atoi(itr->second.at(4).c_str());
	//		_CardInfo.base_magic_defense = atoi(itr->second.at(5).c_str());
	//		_CardInfo.base_heath = atoi(itr->second.at(6).c_str());
	//		_CardInfo.reward_attack_power_per_level = atoi(itr->second.at(7).c_str());
	//		_CardInfo.reward_magic_power_per_level = atoi(itr->second.at(8).c_str());
	//		_CardInfo.reward_physics_defense_per_level = atoi(itr->second.at(9).c_str());
	//		_CardInfo.reward_magic_defense_per_level = atoi(itr->second.at(10).c_str());
	//		_CardInfo.reward_heath_per_level = atoi(itr->second.at(11).c_str());
	//		_CardInfo.card_spell = atoi(itr->second.at(12).c_str());
	//		_CardInfo.feed_reward_exp = atoi(itr->second.at(13).c_str());
	//		_CardInfo.isherocard = atoi(itr->second.at(14).c_str());
	//		_CardInfo.image_url = itr->second.at(15);
	//		_CardInfo.headimage_url = itr->second.at(16);
	//		_CardTemplate[atoi(itr->second.at(0).c_str())] = _CardInfo;
	//	}
	//}
}
void Database::LoadSpellTemplate()
{

}